<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/14
 * Time: 17:18
 */

namespace app\lnadmin\model\admin;
use app\common\Common;
use app\lnadmin\controller\Upload;
use app\lnadmin\model\core\InitModel;
use think\model\concern\SoftDelete;

class User extends InitModel
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    // 定义表
    protected $name = 'user';

    // 第一自动写入时间字段
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

   
    // 可用字段
    protected static $available = [
        'id',
        'group_id',
        'username',
        'qq',
        'password',
        'salt',
        'openid',
        'email',
        'mobile',
        'level',
        'bio',
        'money',
        'score',
        'successions',
        'maxsuccessions',
        'prev_time',
        'login_time',
        'login_num',
        'loginip',
        'loginfailure',
        'create_time',
        'update_time',
        'token',
        'status',
    ];

    
   // 查询人数
    public static function inlineIp()
    {

       return self::where(['status' => 1])->comment('查询人数')->count();
       
    }


   public function userToken()
    {
        return $this->belongsTo(UserToken::class, 'id', 'user_id')->bind([
                'user_id',
            ]);
    }

    /**
     * 单个用户信息
     * @param $uid
     * @return array|bool|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: MK
     * @Time: 2020/4/12 17:24
     */
    public static function findUser($user_id)
    {
        if (!$user_id) return false;
        $user = self::where('id', $user_id)->field([
            'id',
            'qq',
            'username',
            'group_id',
            'mobile',
            'email',
            'openid',
            'level',
            'bio',
            'money',
            'score',
            'sex',
            'login_time',
            'exp_time',
            'maxsuccessions',
            'successions',
            'prev_time',
        ])->find();
         
        if (!empty($user->id)) {
            $user->avatar = Image::image($user->id, Upload::TYPE_USER);//会员头像
            $user->tar = self::where(['id' => $user_id, 'status' => 0])->find();//锁定会员
        }
        return $user;
    }

    /**
     * 编辑
     * @param $post
     * @return bool
     * @author: MK
     * @Time: 2020/4/5 12:49
     */
     public static function edit($post)
    {
        try {
            $id = (int)$post['id'];
            $model = self::where(['id' => $id])->find();
            if (!$model) return false;
            $model->save($post);  
         //保存图片
            if (!empty($post['image'])) {
             Image::add([
                    'table_id' => $id,
                    'url' => $post['image'],
                    'types' => Upload::TYPE_USER,
                    'file_size' => $post['filesize'],
                    'mine' => $post['mine']
             ]);
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
    // 新增
    public static function add($post)
    {
        if (isset($post['id'])) unset($post['id']);
        try {
            $model = new self();
            foreach ($post as $key => $val) {
                if (in_array($key, self::$available)) {
                    $model->$key = $val;
                }
            }
            $model->save();
            //保存图片
            if (!empty($post['image'])) {
                Image::create([
                    'table_id' => $model->id,
                    'url' => $post['image'],
                    'types' => Upload::TYPE_USER,
                    'file_size' => $post['filesize'],
                    'mine' => $post['mine']
             ]);
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }  
}